package com.cictec.middleware.gps.engine.position.handler.alarms;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cictec.middleware.commons.utils.Constants;
import com.cictec.middleware.commons.utils.DateUtils;
import com.cictec.middleware.commons.utils.PMSUtils;
import com.cictec.middleware.commons.utils.PropertiesUtils;
import com.cictec.middleware.core.entity.DeparterSchedule;
import com.cictec.middleware.core.entity.WarnMsg;
import com.cictec.middleware.gps.engine.BusInstance;
import com.cictec.middleware.gps.engine.exception.BusPositionWarnHandlerException;
import com.cictec.middleware.gps.engine.position.handler.BusPositionAlarmsHandler;
import com.cictec.middleware.gps.service.ScheduleService;
import com.cictec.middleware.gps.service.WarnMsgService;

/**
 * 延迟发车报警
 * @author ql
 * @date 2017年9月26日
 */
@Component
public class DepartDelayWarnHandler implements WarnHandler {


	private static Logger logger = LoggerFactory.getLogger(BusPositionAlarmsHandler.class);
	
	@Autowired
	private WarnMsgService warnMsgService;
	@Autowired
	private ScheduleService scheduleService;
	
	
	@Override
	public String getWarnType() {
		return Constants.VAL_BUS_STATUS_12;
	}
	
	@Override
	public void onHandler(BusInstance busInstance) throws BusPositionWarnHandlerException {
		
		logger.debug("【延迟发车报警信息处理】");
		
		if(!busInstance.isEnterOut()){
			return;
		}
		Date currentTime = busInstance.getTime();
		DeparterSchedule ds = scheduleService.getDeparterSchedule(busInstance.getLineUuid(), busInstance.getLineType(), busInstance.getBusUuid(), currentTime, 0, false);
		if(ds == null){
			return;
		}
		
		int delaySeconds = PropertiesUtils.getIntValue("system.parameter.warn.delaySeconds", 60*5);
		long cha = DateUtils.secondInterval(ds.getDepartureTime(), currentTime);
		if(currentTime.getTime() > ds.getDepartureTime().getTime()){
			// 过了发车时间,并且没有第一站出站信息
			String seq = busInstance.isEnterOut() ? busInstance.getStationLineSeqOrg() : "";
			if(cha < delaySeconds && !seq.equals("1")){
				startWarn(busInstance);
			}else{
				stopWarn(busInstance);
			}
		}else if(cha > delaySeconds){
			stopWarn(busInstance);
		}
	}
	
	public void startWarn(BusInstance busInstance){

		// 记录延迟发车
		WarnMsg warnMsg = busInstance.getWarnsByWarnType(Constants.VAL_BUS_STATUS_12);
		if(warnMsg == null){
			warnMsg = new WarnMsg();
			warnMsg.setWarnBusUuid(busInstance.getBusUuid());
			warnMsg.setWarnDevUuid(busInstance.getDevUuid());
		}
		if(!PMSUtils.isEmpty(busInstance.getDriverUuid())){
			warnMsg.setWarnDrvUuid(busInstance.getDriverUuid());
		}
		warnMsg.setWarnLineUuid(busInstance.getLineUuid());
		warnMsg.setWarnStartTime(DateUtils.formatDateTimes(busInstance.getTime()));
		// 延迟发车
		warnMsg.setWarnType(Constants.VAL_BUS_STATUS_12);
		logger.debug("【延迟发车】");
		warnMsg.setWarnStorageTime(DateUtils.formatDateTimes(new Date()));
		
		warnMsgService.saveWarn(warnMsg);
		
		busInstance.setWarns(Constants.VAL_BUS_STATUS_12, warnMsg);
	}

	@Override
	public void stopWarn(BusInstance busInstance) {
		WarnMsg warnMsg = busInstance.getWarnsByWarnType(Constants.VAL_BUS_STATUS_12);
		if(warnMsg != null){
			warnMsg.setWarnEndTime(DateUtils.formatDateTimes(busInstance.getTime()));
			warnMsgService.saveWarn(warnMsg);
			
			busInstance.setWarns(Constants.VAL_BUS_STATUS_12, null);
		}
	}

}
